/**
 * 折线图
 */
var faultAvgCount; // 折线图
var xdata = []; // x轴数据
var ydata0 = [];//110kV
var ydata1 = []; // 200kV
var ydata2 = []; // 500kV

/**
 * 饼形图
 */
var faultDetailpie; // 饼形图
var pieData = [];
var maxValue = 0;//所有数值中的最大值
var avg110 = 0;//
var avg500 = 0;
var avg220 = 0;
var faultFastRemoveRatio = 0;

//chart查询详情
var chartSatrtYear;
var chartEndYear;
var chartVoltage;
var table;
var timeColumns = [ "", "设备名称", "时间", "设备类型", "电压", "故障类型", "故障相别", "详情", "持续时间" ];
var breakerColumns = [ "", "变电站名称","线路名称","断路器名称","故障时间","相别","动作类型","断路器动作时间间隔（ms）"];

$(function() {
	initYear();
	initQueryYear();
	getQueryData();
});

function initYear() {
	var date = new Date();
	var currentYear = date.getFullYear();
	$("#startYear").val(currentYear);
	$("#startYear").datetimepicker({
		startView : 4,
		minView : 4,
		autoclose : true,
		format : 'yyyy'
	});
	$("#endYear").val(currentYear);
	$("#endYear").datetimepicker({
		startView : 4,
		minView : 4,
		autoclose : true,
		format : 'yyyy'
	});
}

function initQueryYear() {
	var d = new Date();
	var currentYear = d.getFullYear();
	$("#mYear").val(currentYear);
	$('#mYear').datetimepicker({
		startView : 4,
		minView : 4,
		autoclose : true,
		format : 'yyyy'
	});
	chartSatrtYear = currentYear;
	chartEndYear = currentYear;
}

function changeFaultQueryYear() {
	var mYear = $("#mYear").val();
	selectFaultQueryYear([ {
		name : 'mYear',
		value : mYear
	} ]);
	chartSatrtYear = mYear;
	chartEndYear = mYear;
}

function getQueryData() {
	var startYear = $("#startYear").val();
	var endYear = $("#endYear").val();
	chartSatrtYear = startYear;
	chartEndYear = endYear;
	avg500 = 0;
	avg220 = 0;
	avg110 = 0;
	maxValue = 0;
	var indexV = document.getElementById("faultQueryForm:vol").selectedIndex; // 获取当前选择项的索引
	var voltage = document.getElementById("faultQueryForm:vol").options[indexV].value; // 获取当前选择项的值
	chartVoltage = voltage;
	var year = startYear == endYear ? startYear : startYear + "~" + endYear;
	var vol = voltage == 0 ? "220kV及以上" : voltage + "kV";
	$.getJSON("/rest/fault/getFaultContinuedTimeCount/" + startYear + "/"+ endYear + "/" + voltage, function(result) {
		initChartData(result, startYear, endYear); // 获取x、y轴数据
		var title = year + "年" + vol + "系统故障平均持续时间";
		faultAvgCount = initChart('faultAvgCount', title, voltage);
	});

	$.ajaxSettings.async = false;
	$.getJSON("/rest/fault/getFaultContinuedTimeDetailfromOMS/" + startYear+ "/" + endYear + "/" + voltage, function(result) {
		initFaultPieData(result, voltage);
		var title = year + "年" + vol + "系统故障持续时间";
		faultDetailpie = initPie('faultDetail', title);
	});

	$.getJSON("/rest/fault/getFaultFastRemoveFromOms/" + startYear + "/" + endYear+ "/" + voltage, function(result) {
		faultFastRemoveRatio = result;
	});

	initCountInfo(startYear, endYear, voltage);
	$.ajaxSettings.async = true;
}

function initCountInfo(startYear, endYear, voltage) {
	var year = startYear == endYear ? startYear : startYear + "~" + endYear;
	var vol = voltage == 0 ? "220kV及以上" : voltage + "kV";
	var timeDistance = avg220-avg500;
	var build = "";
	$("#a_title").html(year + '年' + vol + '系统');
	if (voltage == 0) {
		$("#a_li1").html('故障平均持续时间：'+avg500+'ms（500kV），'+avg220+'ms（220kV）。');//+avg110+'ms（110kV）。'
		build = '，500kV系统故障平均持续时间比220kV系统短 '+timeDistance.toFixed(2)+'ms。';
	} else if (voltage == 500) {
		$("#a_li1").html('故障平均持续时间：' + avg500 + 'ms（500kV）。');
	} else if (voltage == 220) {
		$("#a_li1").html('故障平均持续时间：' + avg220 + 'ms（220kV）。');
	} else if (voltage == 110) {
		$("#a_li1").html('故障平均持续时间：' + avg110 + 'ms（110kV）。');
	}
	if(build==""){
		$("#a_li2").html('系统故障持续时间在（'+maxValue+'ms）以内'+ '，故障快切率（'+faultFastRemoveRatio.toFixed(2)+'%）。'+ build);
	}else{
		$("#a_li2").html('系统故障持续时间在（'+maxValue+'ms）以内'+ '，故障快切率（'+faultFastRemoveRatio.toFixed(2)+'%）'+ build);
	}
}

function initChart(chartId, title, voltage) {
	var line = new CategoryLine(chartId);//Category2YLine
	line.setxAxis(xdata);
	if (voltage == 0) {//line.setyAxis(yAxisData);
//		line.setyAxisTitle("ms");
//		line.setyAxis(ydata0);
//		line.setyAxisName("110kV");

		line.setyAxisTitle("ms");
		line.setyAxis(ydata1);
		line.setyAxisName("220kV");

		line.setyAxisTitle("ms");
		line.setyAxis(ydata2);
		line.setyAxisName("500kV");

	} else if (voltage == 220) {
		line.setyAxisTitle("ms");
		line.setyAxis(ydata1);
		line.setyAxisName("220kV");
	} else if (voltage == 500) {
		line.setyAxisTitle("ms");
		line.setyAxis(ydata2);
		line.setyAxisName("500kV");
	} else if (voltage == 110) {
		line.setyAxisTitle("ms");
		line.setyAxis(ydata0);
		line.setyAxisName("110kV");
	}
	line.setTitle(title);
	var option = line.getOption();
	option.grid.top = 80;
	option.title.textStyle.fontSize = 16;
	option.title.textStyle.fontWeight = 'normal';
	option.title.right = "center";
	option.legend.top = 30;
	line.show();
	line.getChart().on(//TODO
			"click",
			function(params) {
				var month = params.name.substring(0,params.name.length-1);
				  onChartClick(chartSatrtYear, chartEndYear, chartVoltage , 0 , 100 , chartId,month);
			});
	return line;
}

function initChartData(data, startYear, endYear) {
	if (startYear == endYear) {
		// 展示1-12月份的数据
		initChartMonthData(data);
	} else {
		// 展示startYear到endYear的年度数据
		initChartYearData(data, startYear, endYear);
	}
}

function initChartYearData(data, startYear, endYear) {
	xdata = [];
	ydata0 = [];
	ydata1 = [];
	ydata2 = [];
	for (var year = startYear; year <= endYear; year++) {
		xdata.push(year);
		ydata0.push(0);
		ydata1.push(0);
		ydata2.push(0);
	}
	for ( var i in data) {
		var d = data[i];
		var year = d[0];
		var vol = d[2];
		if (vol == 220) {
			ydata1.splice(year - startYear, 1, d[3]);
		} else if (vol == 500) {
			ydata2.splice(year - startYear, 1, d[3]);
		} else if (vol == 110) {
			ydata0.splice(year - startYear, 1, d[3]);
		}
	}
}

function initChartMonthData(data) {
	//截止到当前月
	var currentMonth=new Date().getMonth()+1;
	xdata=[];ydata0=[];ydata1=[];ydata2=[];
	for(var i=1;i<=currentMonth;i++){
		xdata.push(i+"月");
		ydata0.push(0);
		ydata1.push(0);
		ydata2.push(0);
	}
	//  [2018, 4, 220, 31.2]
//	xdata = [ '1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月',
//			'11月', '12月' ]; // 月份
//	ydata0 = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]; // 110kV
//	ydata1 = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]; // 200kV
//	ydata2 = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]; // 500kV
	for ( var i in data) {
		var d = data[i];
		var month = d[1];
		if(month>currentMonth){
			continue;
		}
		var vol = d[2];
		if (vol == 220) {
			ydata1.splice(month - 1, 1, d[3]);
		} else if (vol == 500) {
			ydata2.splice(month - 1, 1, d[3]);
		} else if (vol == 110) {
			ydata0.splice(month - 1, 1, d[3]);
		}
	}
}

function initPie(chartId, title) {
	faultDetailpie = new Pie(chartId);
	faultDetailpie.setPie(pieData).setPieCenter('55%', '55%');
	var option = faultDetailpie.getOption();
	option.grid.top = 80;
	option.title.textStyle.fontSize = 16;
	option.title.textStyle.fontWeight = 'normal';
	option.legend.orient = 'vertical';
	faultDetailpie.setPieOuterRadius('50%').setLegendPosition('left', 'center')
			.setTitle(title).setPieLabelFormatter("{d}%").setTitlePosition(
					'center');
	faultDetailpie.show();
	faultDetailpie.getChart().on(
			"click",
			function(params) {
				var range = params.data["name"].split(',');
				var range1 = range[0].substring(1, range[0].length - 2);
				var range2 = range[1].substring(0, range[1].length - 3);
			    onChartClick(chartSatrtYear, chartEndYear, chartVoltage, range1 , range2 , chartId,0);
			});
}

function initFaultPieData(data, voltage) {
	pieData = [];
	var max = 90;
	var legend40 = "[" + 0 + "ms," + 40 + "ms)";
	pieData.push([ legend40, 0 ]);
	for (var j = 40; j < max; j += 10) {
		var legend = "[" + j + "ms," + (j + 10) + "ms)";
		pieData.push([ legend, 0 ]);
	}
	if (voltage == 0) {
		pieData = initFaultPieDataAll(data, max);
	} else {
		pieData = initFaultPieDataByVol(data, max, voltage);
	}
}

function initFaultPieDataByVol(data, max, voltage) {
	var sumTime = 0; //当前电塔等级下数据的故障持续时间总和
	var num = 0;
	maxValue = 0;
	for ( var i in data) {
		var d = data[i];
		var vol = d[2];
		var index=0;
		if (vol == voltage) {
			var t = Number(d[3]);
			if (t <= max) {
				sumTime += t;
				num++;
//				var index= Math.ceil(t / 10);
				if(t%10==0){
					index =t/10 +1;
				}else{
					index = Math.ceil(t / 10);
				}
				if (t > maxValue) {
					maxValue = t;
				}
				if (index < 5) {
					pieData[0][1]++;
				} else if(index>9){
					continue;
				}else {
					pieData[index - 4][1]++;
				}
			}
		}
	}
	if (num != 0) {
		var avg = sumTime / num;
		if (voltage == 220) {
			avg220 = avg.toFixed(2);
		} else if (voltage == 500) {
			avg500 = avg.toFixed(2);
		} else if (voltage == 110) {
			avg110 = avg.toFixed(2);
		}
	}
	return pieData;
}

function initFaultPieDataAll(data, max) {
	var sumTime500 = 0; //500kV故障持续时间、保护动作时间总和
	var sumTime220 = 0; //220kV故障持续时间、保护动作时间总和
	var sumTime110 = 0;
	var num500 = 0;
	var num220 = 0;
	var num110 = 0;
	maxValue = 0;
	for ( var i in data) {
		var d = data[i];
		var t = Number(d[3]);
		var index=0;
		if (!isNaN(t) && t <= max) {
			if (d[2] == 500) {
				sumTime500 += t;
				num500++;
			} else if (d[2] == 220) {
				sumTime220 += t;
				num220++;
			} else if (d[2] == 110) {
				sumTime110 += t;
				num110++;
			}
			if(t%10==0){
				index =t/10 +1;
			}else{
				index = Math.ceil(t / 10);
			}
//			var index = Math.floor(t / 10);
			if (t > maxValue) {
				maxValue = t;
			}
			if (index < 5) {
				pieData[0][1]++;
			} else if(index>9){
				continue;
			} else {
				pieData[index - 4][1]++;
			}
		}
	}
	maxValue = maxValue.toFixed(2);
	if (num220 != 0) {
		avg220 = sumTime220 / num220;
		avg220 = avg220.toFixed(2);
	}
	if (num500 != 0) {
		avg500 = sumTime500 / num500;
		avg500 = avg500.toFixed(2);
	}
	if (num110 != 0) {
		avg110 = sumTime110 / num110;
		avg110 = avg110.toFixed(2);
	}
	return pieData;
}

// *******************************保护动作时间******************************************

/**
 * 折线图
 */
var protectActAvg;
/**
 * 饼形图
 */
var protectDetailpie;
/**
 * 20ms以内动作的占比
 */
var percent;

$("#protectTab").click(function() {
	var date = new Date();
	var currentYear = date.getFullYear();
	$("#startYear1").val(currentYear);
	$("#startYear1").datetimepicker({
		startView : 4,
		minView : 4,
		autoclose : true,
		format : 'yyyy'
	});
	$("#endYear1").val(currentYear);
	$("#endYear1").datetimepicker({
		startView : 4,
		minView : 4,
		autoclose : true,
		format : 'yyyy'
	});
	getProtectQueryData();
});

//保护查询数据
function getProtectQueryData() {
	avg500 = 0;
	avg220 = 0;
	avg110 = 0;
	maxValue = 0;
	var startYear = $("#startYear1").val();
	var endYear = $("#endYear1").val();
	chartSatrtYear = startYear;
	chartEndYear = endYear;
	var indexV = document.getElementById("protectQueryForm:vol1").selectedIndex;
	var voltage = document.getElementById("protectQueryForm:vol1").options[indexV].value;
	chartVoltage = voltage;
	var year = startYear == endYear ? startYear : startYear + "~" + endYear;
	var vol = voltage == 0 ? "220kV及以上" : voltage + "kV";
	$.getJSON("/rest/fault/getProtectActTimeCount/" + startYear + "/" + endYear+ "/" + voltage, function(result) {
		initChartData(result, startYear, endYear); // 获取x、y轴数据
		var title = year + "年" + vol + "系统主保护平均动作时间";
		protectActAvg = initChart('protectAvgCount', title, voltage);
	});
	$.getJSON("/rest/fault/getProtectActTimeDetailfromOMS/" + startYear + "/"+ endYear + "/" + voltage, function(result) {
		initProtectPieData(result, voltage);
		var title = year + "年" + vol + "系统主保护动作时间";
		protectDetailpie = initPie('protectDetail', title);
		initProtectActInfo(startYear, endYear, voltage);
	});
}

function initProtectPieData(data, voltage) {//TODO
	pieData = [];
	var max = 40;
	for (var j = 0; j < max; j += 5) {
		var legend = "[" + j + "ms," + (j + 5) + "ms)";
		pieData.push([ legend, 0 ]);
	}
	if (voltage == 0) {
		pieData = initPieDataAll(data, max);
	} else {
		pieData = initPieDataByVol(data, max, voltage);
	}
}

function initPieDataByVol(data, max, voltage) {
	var sumTime = 0; //220kV故障持续时间总和
	var num = 0;
	maxValue = 0;
	for ( var i in data) {
		var d = data[i];
		var vol = d[2];
		var index=0;
		if (vol == voltage) {
			var t = Number(d[3]);
			if (t <= max) {
				sumTime += t;
				num++;
				 index = Math.floor(t / 5);
				if (t > maxValue) {
					maxValue = t;
				}
				if (pieData[index]) {
					pieData[index][1]++;
				}

			}
		}
	}
	var floor =0;;
	var allNum =0;
	for (var i in pieData) {
		var d = pieData[i];
		if(i<=3){
			floor+=d[1];
		}
		allNum+=d[1];
	} 
	percent = (floor/allNum*100);
	if (num != 0) {
		var avg = sumTime / num;
		if (voltage == 220) {
			avg220 = avg.toFixed(2);
		} else if (voltage == 500) {
			avg500 = avg.toFixed(2);
		} else if (voltage == 110) {
			avg110 = avg.toFixed(2);
		}
	}
	return pieData;
}

function initPieDataAll(data, max) {
	var sumTime500 = 0; //500kV故障持续时间、保护动作时间总和
	var sumTime220 = 0; //220kV故障持续时间、保护动作时间总和
	var sumTime110 = 0; //110kV故障持续时间、保护动作时间总和
	var num500 = 0;
	var num220 = 0;
	var num110 = 0;
	maxValue = 0;
	for ( var i in data) {
		var d = data[i];
		var t = Number(d[3]);
		if (!isNaN(t) && t <= max) {
			if (d[2] == 500) {
				sumTime500 += t;
				num500++;
			} else if (d[2] == 220) {
				sumTime220 += t;
				num220++;
			} else if (d[2] == 110) {
				sumTime110 += t;
				num110++;
			}
			var index = Math.floor(t / 5);
			if (t > maxValue) {
				maxValue = t;
			}
			if (pieData[index]) {
				pieData[index][1]++;
			}
		}
	}
	var floor =0;;
	var allNum =0;
	for (var i in pieData) {
		var d = pieData[i];
		if(i<=3){
			floor+=d[1];
		}
		allNum+=d[1];
	} 
	percent = (floor/allNum*100);
	maxValue = maxValue.toFixed(2);
	if (num220 != 0) {
		avg220 = sumTime220 / num220;
		avg220 = avg220.toFixed(2);
	}
	if (num500 != 0) {
		avg500 = sumTime500 / num500;
		avg500 = avg500.toFixed(2);
	}
	if (num110 != 0) {
		avg110 = sumTime110 / num110;
		avg110 = avg110.toFixed(2);
	}
	return pieData;
}

function initProtectActInfo(startYear, endYear, voltage) {
	var year = startYear == endYear ? startYear : startYear + "~" + endYear;
	var vol = voltage == 0 ? "220kV及以上" : voltage + "kV";
	var build ; 
	$("#b_title").html(year + '年' + vol + "系统");
	if(percent!=100){
		build = Math.floor(percent)+'%以上故障动作时间在20ms以内，'+'最长动作时间（'+maxValue+'ms），'+'220kV、500kV主保护平均动作时间分布特点相似。';
	}else{
		build = Math.floor(percent)+'%故障动作时间在20ms以内，'+'最长动作时间（'+maxValue+'ms），'+'220kV、500kV主保护平均动作时间分布特点相似。';
	}
	if (voltage == 0) {
		$("#b_li1").html('平均动作时间：'+avg500+'ms（500kV），'+avg220+'ms（220kV）。');//+avg110+'ms（110kV）。'
	} else if (voltage == 500) {
		$("#b_li1").html('平均动作时间：' + avg500 + 'ms（500kV）。');
	} else if (voltage == 220) {
		$("#b_li1").html('平均动作时间：' + avg220 + 'ms（220kV）。');
	} else if (voltage == 110) {
		$("#b_li1").html('平均动作时间：' + avg110 + 'ms（110kV）。');
	}
	$("#b_li2").html(build);
}

function initProtectQueryYear() {
	var d = new Date();
	var currentYear = d.getFullYear();
	$("#mYear1").val(currentYear);
	$('#mYear1').datetimepicker({
		startView : 4,
		minView : 4,
		autoclose : true,
		format : 'yyyy'
	});
}

function changeProtectQueryYear() {
	var mYear = $("#mYear1").val();
	selectFaultQueryYear([ {
		name : 'mYear',
		value : mYear
	} ]);
}

// *******************************断路器动作时间******************************

/**
 * 折线图
 */
var breakerActAvg;
/**
 * 饼形图
 */
var breakerDetailPie;

$("#breakerTab").click(function() {
	var date = new Date();
	var currentYear = date.getFullYear();
	$("#startYear2").val(currentYear);
	$("#startYear2").datetimepicker({
		startView : 4,
		minView : 4,
		autoclose : true,
		format : 'yyyy'
	});
	$("#endYear2").val(currentYear);
	$("#endYear2").datetimepicker({
		startView : 4,
		minView : 4,
		autoclose : true,
		format : 'yyyy'
	});
	getBreakerQueryData();
});

function initBreakerActInfo(startYear, endYear, voltage) {
	var year = startYear == endYear ? startYear : startYear + "~" + endYear;
	var vol = voltage == 0 ? "220kV及以上" : voltage + "kV";
	$("#c_title").html(year + '年' + vol + "系统");
	if (voltage == 0) {
		$("#c_li1").html('断路器平均动作时间：'+avg500+'ms（500kV），'+avg220+'ms（220kV）。');//+avg110+'ms（110kV）。'
		var dValue = avg500-avg220;
		$("#c_li2").html('500kV 断路器与220kV断路器平均动作时间差（'+Math.abs(dValue).toFixed(2)+'ms）'+'，与近年来的统计数值持平。');
	} else if (voltage == 500) {
		$("#c_li1").html('断路器平均动作时间：' + avg500 + 'ms（500kV）。');
	} else if (voltage == 220) {
		$("#c_li1").html('断路器平均动作时间：' + avg220 + 'ms（220kV）。');
	} else if (voltage == 110) {
		$("#c_li1").html('断路器平均动作时间：' + avg110 + 'ms（110kV）。');
	}
}

function getBreakerQueryData() {
	avg500 = 0;
	avg220 = 0;
	avg110 = 0;
	var startYear = $("#startYear2").val();
	var endYear = $("#endYear2").val();
	chartSatrtYear = startYear;
	chartEndYear = endYear;
	var indexV = document.getElementById("breakerQueryForm:vol2").selectedIndex;
	var voltage = document.getElementById("breakerQueryForm:vol2").options[indexV].value;
	chartVoltage = voltage;
	var year = startYear == endYear ? startYear : startYear + "~" + endYear;
	var vol = voltage == 0 ? "220kV及以上" : voltage + "kV";
	$.getJSON("/rest/fault/getBreakerActTimeCount/" + startYear + "/" + endYear
			+ "/" + voltage, function(result) {
		initChartData(result, startYear, endYear); // 获取x、y轴数据
		var title = year + "年" + vol + "系统断路器平均动作时间";
		breakerActAvg = initChart('breakerCount', title, voltage);
	});
	$.getJSON("/rest/fault/getBreakerActTimeDetailFromOMS/" + startYear + "/"
			+ endYear + "/" + voltage, function(result) {
		initBreakerPieData(result, voltage);
		var title = year + "年" + vol + "断路器动作时间分布图";
		breakerDetailPie = initPie('breakerDetail', title);
		initBreakerActInfo(startYear, endYear, voltage);
	});
}

function initBreakerPieData(data, voltage) {
	pieData = [];
	var max = 70;
	for (var j = 0; j < max; j += 10) {
		var legend = "[" + j + "ms," + (j + 10) + "ms)";
		pieData.push([ legend, 0 ]);
	}
	if (voltage == 0) {
		pieData = initBreakPieDataAll(data, max);
	} else {
		pieData = initBreakerPieDataByVol(data, max, voltage);
	}
}


function initBreakPieDataAll(data, max) {
	var sumTime500 = 0; //500kV故障持续时间、保护动作时间总和
	var sumTime220 = 0; //220kV故障持续时间、保护动作时间总和
	var sumTime110 = 0; //110kV故障持续时间、保护动作时间总和
	var num500 = 0;
	var num220 = 0;
	var num110 = 0;
	maxValue = 0;
	for ( var i in data) {
		var d = data[i];
		var t = Number(d[3]);
		if (!isNaN(t) && t <= max) {
			if (d[2] == 500) {
				sumTime500 += t;
				num500++;
			} else if (d[2] == 220) {
				sumTime220 += t;
				num220++;
			} else if (d[2] == 110) {
				sumTime110 += t;
				num110++;
			}
			var index = Math.floor(t / 10);
			if (t > maxValue) {
				maxValue = t;
			}
			if (pieData[index]) {
				pieData[index][1]++;
			}
		}
	}
	var floor =0;;
	var allNum =0;
	for (var i in pieData) {
		var d = pieData[i];
		if(i<=3){
			floor+=d[1];
		}
		allNum+=d[1];
	} 
	percent = (floor/allNum*100);
	maxValue = maxValue.toFixed(2);
	if (num220 != 0) {
		avg220 = sumTime220 / num220;
		avg220 = avg220.toFixed(2);
	}
	if (num500 != 0) {
		avg500 = sumTime500 / num500;
		avg500 = avg500.toFixed(2);
	}
	if (num110 != 0) {
		avg110 = sumTime110 / num110;
		avg110 = avg110.toFixed(2);
	}
	return pieData;
}

function initBreakerPieDataByVol(data, max, voltage) {
	var sumTime = 0; //220kV故障持续时间总和
	var num = 0;
	maxValue = 0;
	for ( var i in data) {
		var d = data[i];
		var vol = d[2];
		var index=0;
		if (vol == voltage) {
			var t = Number(d[3]);
			if (t <= max) {
				sumTime += t;
				num++;
				 index = Math.floor(t / 10);
				if (t > maxValue) {
					maxValue = t;
				}
				if (pieData[index]) {
					pieData[index][1]++;
				}

			}
		}
	}
	var floor =0;;
	var allNum =0;
	for (var i in pieData) {
		var d = pieData[i];
		if(i<=3){
			floor+=d[1];
		}
		allNum+=d[1];
	} 
	percent = (floor/allNum*100);
	if (num != 0) {
		var avg = sumTime / num;
		if (voltage == 220) {
			avg220 = avg.toFixed(2);
		} else if (voltage == 500) {
			avg500 = avg.toFixed(2);
		} else if (voltage == 110) {
			avg110 = avg.toFixed(2);
		}
	}
	return pieData;
}





function initBreakerQueryYear() {
	var d = new Date();
	var currentYear = d.getFullYear();
	$("#mYear2").val(currentYear);
	$('#mYear2').datetimepicker({
		startView : 4,
		minView : 4,
		autoclose : true,
		format : 'yyyy'
	});
}

function changeBreakerQueryYear() {
	var mYear = $("#mYear2").val();
	selectFaultQueryYear([ {
		name : 'mYear',
		value : mYear
	} ]);
}

//故障持续时间/保护动作时间 -- chart点击事件  
function onChartClick(startYear, endYear, voltage, range1, range2,chartId,month) {
		$.getJSON(
				"/rest/fault/getDetails/"+ startYear + "/" + endYear + "/" + voltage + "/"+ range1 + "/" + range2+ "/" + chartId+ "/" + month ,function(result) {
					loadTable(result);
				});
		PF('detailDialog').show();
}

//故障持续时间/保护动作时间 -- chart点击事件  列表渲染
function loadTable(detailDatas) {
	if (table) {
		table.getTable().destroy();
	}
	var opt = MyTable.getDefaultOptions();
	opt.paging = false;
	opt.autoWidth = false;
	var defs = [ {
		"width" : "5%",
		"targets" : 0
	},{
		"width" : "8%",
		"targets" : 1
	}, {
		"width" : "15%",
		"targets" : 2
	}, {
		"width" : "8%",
		"targets" : 3
	}, {
		"width" : "8%",
		"targets" : 4
	}, {
		"width" : "8%",
		"targets" : 5
	}, {
		"width" : "8%",
		"targets" : 6
	}, {
		"width" : "30%",
		"targets" : 7
	}, {
		"width" : "12%",
		"targets" : 8
	} ];
	opt.scrollY = 600 + "px";
	opt.scrollX = false;
	var datas = [];
	for ( var i in detailDatas) {
		var f = detailDatas[i];
		datas.push([ Number(i) + 1, str(f[0]), time(f[1]), str(f[2]),
				str(f[3]), str(f[4]), str(f[5]), str(f[6]), f[7] ]);
	}
	var targets = [];
	// 筛选列
	var eles = document.getElementsByName('columns');
	var targets = [];
	for (var i = 0; i < eles.length; i++) {
		if (!eles[i].checked) {
			targets.push(i);
		}
	}
	defs.push({
		visible : false,
		targets : targets
	});
	opt.columnDefs = defs;
	table = new MyTable("detailTable", timeColumns, opt);
	table.setDataSet(datas).show();
}


function str(v) {
	return v ? v : '';
}
function time(v) {
	if (!v) {
		return '';
	} else {
		return new Date(v.time ? v.time : v).toLocaleString();
	}
}
